home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ML_BME1.ZIP / _LIB_ / FILES.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-11-14  |  5.1 KB  |  230 lines

  1. Unit Files;
  2. {
  3.   Files library v1.0
  4.   by Maple Leaf, 13 Nov 1996
  5.   -------------------------------------------------------------------------
  6.   no comments necessary...
  7. }
  8.  
  9. interface
  10.  
  11. uses Strings, Dos;
  12.  
  13. Const
  14.   OwnDirectory : String   = '';
  15.   OwnFileName  : String   = '';
  16.   OwnPath      : String   = '';
  17.  
  18. function  OnlyFileName (path : string) : string;
  19. { Extract file name from a full path ('????????.???') }
  20.  
  21. function  OnlyName (path : string) : string;
  22. { Extract only the name from a full path ('????????') }
  23.  
  24. function  OnlyExt (path : string) : string;
  25. { Extract extension from the name contained in the full path ('.???') }
  26.  
  27. function  OnlyDir (path : string) : string;
  28. { Extract directory from the full path then add '\' at the end of string (if needs) }
  29.  
  30. function  FMatch(file1,file2 : string) : Boolean;
  31. { Check if files format match (ex. *.EXE match with RARA.EXE) }
  32.  
  33. function OpenForInput(var f:file; filename:string) : Boolean;
  34. { Open file for input . Displays ERRMSG and stops the program if any error
  35.    is found }
  36.  
  37. function OpenForOutput(var f:file; filename:string) : Boolean;
  38. { Open file for output . Displays ERRMSG and stops the program if any error
  39.    is found }
  40.  
  41. function OpenForAppend(var f:file; filename:string) : Boolean;
  42. { Open file for append . Displays ERRMSG and stops the program if any error
  43.    is found }
  44.  
  45. function CloseFile(var f:file) : Boolean;
  46. { Close specified file. If any error is appeared, display ERRORMSG onto
  47.    the screen and stops the program }
  48.  
  49. function EraseFile(var f:file) : Boolean;
  50. { Erase specified file. If any error is appeared, display ERRORMSG onto
  51.    the screen and stops the program }
  52.  
  53. function ExistFile(path:string) : Boolean;
  54. { Checks whether the filename is (or is not) the name of an existing file }
  55.  
  56. procedure MakePath(path:String);
  57. { Create a full path }
  58.  
  59. implementation
  60.  
  61. procedure MakePath(path:String);
  62. var s:string; k:word;
  63. begin {$i-}
  64.   if path[length(path)]='\' then path:=copy(path,1,length(path)-1);
  65.   if (pos('\',path)=0) or (path[length(path)]=':') then
  66.     MkDir(Path)
  67.   else
  68.     MakePath(onlydir(Path));
  69.   MkDir(path); k:=IoResult; InOutRes:=0;
  70. end;
  71.  
  72. function  FMatch(file1,file2 : string) : Boolean;
  73. function  EExpand(s:string):string;
  74. var r:string; k:byte;
  75. begin
  76.   r:=''; k:=0; delete(s,1,1);
  77.   repeat
  78.     inc(k);
  79.     if length(s)>=k then begin
  80.       if s[k]='*' then begin
  81.         r:=r+Strng(3-length(r),63);
  82.         k:=3;
  83.       end else r:=r+s[k];
  84.     end else begin
  85.       r:=r+' ';
  86.     end;
  87.   until k=3;
  88.   EExpand:='.'+UCase(r);
  89. end;
  90. function  EMatch(file1,file2 : string) : Boolean;
  91. var q:boolean; k:byte;
  92. begin
  93.   file1:=EExpand(OnlyExt(file1)); file2:=EExpand(OnlyExt(file2));
  94.   q:=true; k:=0;
  95.   repeat
  96.     inc(k);
  97.     if not((file1[k]=file2[k]) or (file1[k]='?') or (file2[k]='?')) then q:=false;
  98.   until not q or (k=4);
  99.   EMatch:=q;
  100. end;
  101. function  Expand(s:string):string;
  102. var r:string; k:byte;
  103. begin
  104.   r:=''; k:=0;
  105.   repeat
  106.     inc(k);
  107.     if length(s)>=k then begin
  108.       if s[k]='*' then begin
  109.         r:=r+Strng(8-length(r),63);
  110.         k:=8;
  111.       end else r:=r+s[k];
  112.     end else begin
  113.       r:=r+' ';
  114.     end;
  115.   until k=8;
  116.   Expand:=UCase(r);
  117. end;
  118. function  NMatch(file1,file2 : string) : Boolean;
  119. var q:boolean; k:byte;
  120. begin
  121.   file1:=Expand(OnlyName(file1)); file2:=Expand(OnlyName(file2));
  122.   q:=true; k:=0;
  123.   repeat
  124.     inc(k);
  125.     if not((file1[k]=file2[k]) or (file1[k]='?') or (file2[k]='?')) then q:=false;
  126.   until not q or (k=8);
  127.   NMatch:=q;
  128. end;
  129. begin
  130.   file1:=OnlyFileName(file1);
  131.   file2:=OnlyFileName(file2);
  132.   FMatch:=NMatch(file1,file2) and EMatch(file1,file2);
  133. end;
  134.  
  135. function onlyname;
  136. var
  137.   d:dirstr;
  138.   e:extstr;
  139.   n:namestr;
  140. begin
  141.   fsplit(path,d,n,e);
  142.   onlyname:=n;
  143. end;
  144.  
  145. function onlydir;
  146. var
  147.   d:dirstr;
  148.   e:extstr;
  149.   n:namestr;
  150. begin
  151.   fsplit(path,d,n,e);
  152.   if d<>'' then
  153.     if d[length(d)]<>'\' then d:=d+'\';
  154.   onlydir:=d;
  155. end;
  156.  
  157. function onlyext;
  158. var
  159.   d:dirstr;
  160.   e:extstr;
  161.   n:namestr;
  162. begin
  163.   fsplit(path,d,n,e);
  164.   onlyext:=e;
  165. end;
  166.  
  167. function onlyfilename;
  168. begin
  169.   onlyfilename:=onlyname(path)+onlyext(path);
  170. end;
  171.  
  172. function OpenForInput;
  173. begin {$i-}
  174.   if ioresult=0 then;
  175.   inoutres:=0;
  176.   assign(f,filename);
  177.   filemode:=0;
  178.   reset(f,1);
  179.   OpenForInput:=(IOResult=0);
  180. end;
  181.  
  182. function OpenForOutput;
  183. begin {$i-}
  184.   if ioresult=0 then;
  185.   inoutres:=0;
  186.   assign(f,filename);
  187.   filemode:=2;
  188.   rewrite(f,1);
  189.   OpenForOutput:=(IOResult=0);
  190. end;
  191.  
  192. function OpenForAppend;
  193. begin {$i-}
  194.   if ioresult=0 then;
  195.   inoutres:=0;
  196.   assign(f,filename);
  197.   filemode:=2;
  198.   reset(f,1);
  199.   OpenForAppend:=(IOResult=0);
  200. end;
  201.  
  202. function CloseFile;
  203. begin {$i-}
  204.   if ioresult=0 then;
  205.   inoutres:=0;
  206.   close(f);
  207.   CloseFile:=(IOResult=0);
  208. end;
  209.  
  210. function EraseFile;
  211. begin {$i-}
  212.   if ioresult=0 then;
  213.   inoutres:=0;
  214.   erase(f);
  215.   EraseFile:=(IOResult=0);
  216. end;
  217.  
  218. function ExistFile;
  219. var r:SearchRec;
  220. begin
  221.   findfirst(path,$3F,r);
  222.   ExistFile:=DosError=0;
  223. end;
  224.  
  225. begin
  226.   OwnPath:=UCase(ParamStr(0));
  227.   OwnDirectory:=OnlyDir(OwnPath);
  228.   OwnFileName:=OnlyFileName(OwnPath);
  229. end.
  230.